package view;



import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import model.BinomialModel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;

public class BinomialView extends JFrame{

	public JLabel lblSuccess = new JLabel("Input number of successes(x): ");
	public JLabel lblTrials = new JLabel("Input number of trials(n): ");
	public JLabel lblProbability = new JLabel("Input probability of success(P): ");
	public JLabel lblTo= new JLabel("TO");
	
	public JTextField txtSuccess1 = new JTextField(7);
	public JTextField txtSuccess2 = new JTextField(7);
	public JTextField txtTrials = new JTextField(5);
	public JTextField txtProbability = new JTextField(5);
	public JTextField txtBinomialAnswer = new JTextField(5);
	public JTextField txtNSimulation = new JTextField(10);
	public JTextField txtPSimulation = new JTextField(10);

	public JButton btnCompute = new JButton("Compute Binomial Probability");
	public JButton btnAddN = new JButton("Increase Value of N by 1 for:");
	public JButton btnAddP = new JButton("Increase Value of P by 0.01 for:");
	
	public DefaultTableModel model = new DefaultTableModel(); 
	public JTable table = new JTable(model); 
	public JScrollPane scrollPane = new JScrollPane(table);
    
	public GridBagConstraints gbc = new GridBagConstraints();	
	
	public JPanel binomialPanel = new JPanel();
	public JPanel chartPanel = new JPanel();
	
	public BinomialView() {
		binomialPanel.setLayout(new GridBagLayout());

		gbc.gridx = 0;
		gbc.gridy = 0;
		binomialPanel.add(lblSuccess, gbc);
		
		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridx = 1;
		gbc.gridy = 0;
		binomialPanel.add(txtSuccess1, gbc);
		
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx = 1;
		gbc.gridy = 0;
		binomialPanel.add(lblTo, gbc);
		
		gbc.anchor = GridBagConstraints.EAST;
		gbc.gridx = 1;
		gbc.gridy = 0;
		binomialPanel.add(txtSuccess2, gbc);
		
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.gridx = 0;
		gbc.gridy = 1;
		binomialPanel.add(lblTrials, gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 1;
		binomialPanel.add(txtTrials, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 2;
		binomialPanel.add(lblProbability, gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 2;
		binomialPanel.add(txtProbability, gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 3;
		binomialPanel.add(btnCompute, gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 4;
		binomialPanel.add(txtBinomialAnswer, gbc);
		
		gbc.gridwidth = 2;
		gbc.gridx = 0;
		gbc.gridy = 5;
		model.addColumn("x");
		model.addColumn("P(x)");
		scrollPane.setPreferredSize(new Dimension(300,250));
		binomialPanel.add(scrollPane, gbc);
	
		gbc.gridheight = 6;
		gbc.gridx = 2;
		gbc.gridy = 0;
		chartPanel.setPreferredSize(new Dimension(500,350));
		binomialPanel.add(chartPanel, gbc);
		
		
		gbc.fill = GridBagConstraints.NONE;

		gbc.gridheight = 1;
		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridx = 2;
		gbc.gridy = 6;
		btnAddN.setPreferredSize(new Dimension(380, 23));
		binomialPanel.add(btnAddN, gbc);
		
		gbc.anchor = GridBagConstraints.EAST;
		gbc.gridx = 2;
		gbc.gridy = 6;
		binomialPanel.add(txtNSimulation, gbc);
		
		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridx = 2;
		gbc.gridy = 7;
		btnAddP.setPreferredSize(new Dimension(380, 23));
		binomialPanel.add(btnAddP, gbc);
		
		gbc.anchor = GridBagConstraints.EAST;
		gbc.gridx = 2;
		gbc.gridy = 7;
		binomialPanel.add(txtPSimulation, gbc);
		
		this.add(binomialPanel);
		this.setSize(1000, 600);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setTitle("Binomial Probabability Simulator");
		this.setResizable(false);
	}
	
	public void addChartPanel(JPanel chartPanel) {
		chartPanel.setPreferredSize(new Dimension(500,350));
		gbc.gridheight = 6;
		gbc.gridx = 2;
		gbc.gridy = 0;
		binomialPanel.add(chartPanel, gbc);
		this.validate();
	}
	
	public String getProbability() {
		return txtProbability.getText();
	}
	
	public void setProbability(double newProbability) {
		DecimalFormat df4 = new DecimalFormat("#.####");
		txtProbability.setText(Double.toString(Double.valueOf(df4.format(newProbability))));
	}
	
	public String getSuccess1() {
		return txtSuccess1.getText();
	}
	
	public String getSuccess2() {
		return txtSuccess2.getText();
	}
	
	public String getTrials() {
		return txtTrials.getText();
	}
	
	public void setTrials(int newtrial) {
		txtTrials.setText(Integer.toString(newtrial));
	}
	
	public String getNSimulation() {
		return txtNSimulation.getText();
	}
	
	public String getPSimulation() {
		return txtPSimulation.getText();
	}
	
	
	public double getBinomialAnswer() {
		return Double.parseDouble(txtBinomialAnswer.getText());
	}
	
	public DefaultTableModel getTableModel() {
		return model;
	}
	
	public void setBinomialAnswer(double answer) {
		txtBinomialAnswer.setText(Double.toString(answer));
	}
	
	public void addComputeListener(ActionListener listenForComputeButton) {
		btnCompute.addActionListener(listenForComputeButton);
	}
	
	public void addNListener(ActionListener listenForAddNButton) {
		btnAddN.addActionListener(listenForAddNButton);
	}
	
	public void addPListener(ActionListener listenForAddPButton) {
		btnAddP.addActionListener(listenForAddPButton);
	}
	
	public void displayErrorMessage(String message){
		JOptionPane.showMessageDialog(this, message);
	}
	
}
